package com.hhit.bigscreenbackmanage.service.impl.tgygl;

import com.hhit.bigscreenbackmanage.dao.tgygl.TTgyglEngineeringPlanActualSpeedMapper;
import com.hhit.bigscreenbackmanage.entity.tgygl.TTgyglConstructPlanAmiYc;
import com.hhit.bigscreenbackmanage.entity.tgygl.TTgyglEngineeringPlanActualSpeed;
import com.hhit.bigscreenbackmanage.service.tgygl.TTgyglEngineeringPlanActualSpeedService;
import org.seimicrawler.xpath.JXNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author  zhl
 * @Date 2021/7/22
 */
@Service
public class TTgyglEngineeringPlanActualSpeedServiceImpl implements TTgyglEngineeringPlanActualSpeedService {

    @Autowired
    private TTgyglEngineeringPlanActualSpeedMapper tTgyglEngineeringPlanActualSpeedMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return 0;
    }

    @Override
    public int insert(TTgyglEngineeringPlanActualSpeed record) {
        return 0;
    }

    @Override
    public int insertSelective(TTgyglEngineeringPlanActualSpeed record) {
        return 0;
    }

    @Override
    public TTgyglEngineeringPlanActualSpeed selectByPrimaryKey(Integer id) {
        return null;
    }

    @Override
    public int updateByPrimaryKeySelective(TTgyglEngineeringPlanActualSpeed record) {
        return 0;
    }

    @Override
    public int updateByPrimaryKey(TTgyglEngineeringPlanActualSpeed record) {
        return 0;
    }

    @Override
    public void insertWord(JXNode jxNode, int id, int year, int month) throws Exception {

        tTgyglEngineeringPlanActualSpeedMapper.deleteByNfAndFf(year, month);

        List<JXNode> trNodeList = jxNode.sel("//tr");

        //获取第一行的标题 count_plan
        List<JXNode> countPlan = trNodeList.get(0).sel("//td");
        //累计进场数量标题
        String countPlan1 = countPlan.get(1).selOne("//p//text()").toString();
        String countPlan2 = countPlan.get(2).selOne("//p//text()").toString();
        String countPlan3 = countPlan.get(3).selOne("//p//text()").toString();
        String countPlan4 = countPlan.get(4).selOne("//p//text()").toString();

        for (JXNode item : trNodeList) {
            int index = trNodeList.indexOf(item);
            if (index > 0) {
                List<JXNode> tdNodeList = item.sel("//td");
                TTgyglEngineeringPlanActualSpeed tTgyglEngineeringPlanActualSpeed = new TTgyglEngineeringPlanActualSpeed();

                //年
                tTgyglEngineeringPlanActualSpeed.setYear(year);
                //月
                tTgyglEngineeringPlanActualSpeed.setMonth(month);
                //文件id
                tTgyglEngineeringPlanActualSpeed.setFileId(id);
                //备注
                tTgyglEngineeringPlanActualSpeed.setremarks(
                        tdNodeList.get(5).selOne("//p//text()") == null ? "" : tdNodeList.get(5).selOne("//p//text()").toString());

                //第一行遇到'合计就跳过'
                JXNode node = tdNodeList.get(0).selOne("//p//text()");
                if (node == null || (node.toString().contains("合") && node.toString().contains("计"))) {
                    continue;
                }

                //第一个数据
                //地名
                tTgyglEngineeringPlanActualSpeed.setSectionName(node.toString());
                //标题
                tTgyglEngineeringPlanActualSpeed.setCountPlan(countPlan1 == null ? "" : countPlan1);
                //数据
                tTgyglEngineeringPlanActualSpeed.setContent(
                        tdNodeList.get(1).selOne("//p//text()") == null ? "" : tdNodeList.get(1).selOne("//p//text()").toString());

                tTgyglEngineeringPlanActualSpeedMapper.insert(tTgyglEngineeringPlanActualSpeed);

                //第二个数据
                tTgyglEngineeringPlanActualSpeed.setSectionName(node.toString());
                //标题
                tTgyglEngineeringPlanActualSpeed.setCountPlan(countPlan2 == null ? "" : countPlan2);
                //数据
                tTgyglEngineeringPlanActualSpeed.setContent(
                        tdNodeList.get(2).selOne("//p//text()") == null ? "" : tdNodeList.get(2).selOne("//p//text()").toString());

                tTgyglEngineeringPlanActualSpeedMapper.insert(tTgyglEngineeringPlanActualSpeed);

                //第三个数据
                tTgyglEngineeringPlanActualSpeed.setSectionName(node.toString());
                //标题
                tTgyglEngineeringPlanActualSpeed.setCountPlan(countPlan3 == null ? "" : countPlan3);
                //数据
                tTgyglEngineeringPlanActualSpeed.setContent(
                        tdNodeList.get(3).selOne("//p//text()") == null ? "" : tdNodeList.get(3).selOne("//p//text()").toString());

                tTgyglEngineeringPlanActualSpeedMapper.insert(tTgyglEngineeringPlanActualSpeed);

                //第四个数据
                tTgyglEngineeringPlanActualSpeed.setSectionName(node.toString());
                //标题
                tTgyglEngineeringPlanActualSpeed.setCountPlan(countPlan4 == null ? "" : countPlan4);
                //数据
                tTgyglEngineeringPlanActualSpeed.setContent(
                        tdNodeList.get(4).selOne("//p//text()") == null ? "" : tdNodeList.get(4).selOne("//p//text()").toString());

                tTgyglEngineeringPlanActualSpeedMapper.insert(tTgyglEngineeringPlanActualSpeed);

                System.out.println(item);
            }

        }
    }

    @Override
    public List<TTgyglEngineeringPlanActualSpeed> selectListByFileId(Integer fileId) {
        List<TTgyglEngineeringPlanActualSpeed> resulList = null;
        TTgyglEngineeringPlanActualSpeed tTgyglEngineeringPlanActualSpeed = new TTgyglEngineeringPlanActualSpeed();
        tTgyglEngineeringPlanActualSpeed.setFileId(fileId);
        try {
            resulList = tTgyglEngineeringPlanActualSpeedMapper.selectByBean(tTgyglEngineeringPlanActualSpeed);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resulList;
    }
}
